package com.orange.score.service;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.orange.common.android.activity.PPActivity;
import com.orange.score.model.user.UserManager;
import java.util.UUID;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class UserService
{
  private static final String TAG = "UserService";

  public static String httpGet(String paramString)
  {
    DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
    HttpGet localHttpGet = new HttpGet(paramString);
    BasicResponseHandler localBasicResponseHandler = new BasicResponseHandler();
    String str = null;
    try
    {
      Log.i("UserService", "Send http request: " + paramString);
      str = (String)localDefaultHttpClient.execute(localHttpGet, localBasicResponseHandler);
      Log.i("UserService", "Get http response: " + str);
      return str;
    }
    catch (Exception localException)
    {
      Log.i("UserService", "catch exception while send http request " + localException.toString(), localException);
    }
  }

  public void CommitComment(PPActivity paramPPActivity, String paramString)
  {
    CommitCommentTask localCommitCommentTask = new CommitCommentTask(this, paramPPActivity, paramString);
    String[] arrayOfString = new String[1];
    arrayOfString[0] = paramString;
    localCommitCommentTask.execute(arrayOfString);
  }

  public void RegisterUser()
  {
    new RegisterUserTask(this, null).execute(new String[0]);
  }

  public void updateClient(PPActivity paramPPActivity, String paramString1, String paramString2)
  {
    updateClientTask localupdateClientTask = new updateClientTask(this, paramPPActivity, paramString1, paramString2, false);
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "";
    localupdateClientTask.execute(arrayOfString);
  }

  public void updateClientWhenLaunch(PPActivity paramPPActivity, String paramString1, String paramString2)
  {
    updateClientTask localupdateClientTask = new updateClientTask(this, paramPPActivity, paramString1, paramString2, true);
    String[] arrayOfString = new String[1];
    arrayOfString[0] = "";
    localupdateClientTask.execute(arrayOfString);
  }

  private class CommitCommentTask extends AsyncTask<String, Void, String>
  {
    PPActivity activity;
    String url;

    public CommitCommentTask(, PPActivity paramPPActivity, String paramString)
    {
      this.activity = paramPPActivity;
      this.url = paramString;
    }

    protected String doInBackground()
    {
      return UserService.httpGet(this.url);
    }

    protected void onPostExecute()
    {
      this.activity.hideDialog();
      if (paramString == null)
      {
        Log.w("UserService", "commit comment failed.");
        Toast.makeText(this.activity.getApplicationContext(), "网络错误，请检查网络是否连接", 0).show();
      }
      while (true)
      {
        return;
        Log.i("UserService", "commit comment success.respond=" + paramString);
        Toast.makeText(this.activity.getApplicationContext(), "您的反馈已经提交", 0).show();
      }
    }

    protected void onPreExecute()
    {
      this.activity.showProgressDialog("", "信息反馈中...");
    }
  }

  private class RegisterUserTask extends AsyncTask<String, Void, String>
  {
    String url = "http://bf.bet007.com/phone/Register.aspx?kind=2&token=";

    protected String doInBackground()
    {
      return UserService.httpGet(this.url);
    }

    protected void onPostExecute()
    {
      super.onPostExecute(paramString);
      if (paramString == null);
      while (true)
      {
        return;
        Log.i("UserService", "register success, user id =" + paramString);
        UserManager.getInstance().saveUserId(paramString);
      }
    }

    protected void onPreExecute()
    {
      super.onPreExecute();
      Log.i("UserService", "send register: " + this.url);
      this.url += UUID.randomUUID().toString();
    }
  }

  private class updateClientTask extends AsyncTask<String, Void, String>
  {
    PPActivity activity;
    String checkURL;
    boolean isBackgroundUpdate = false;
    String updateURL;

    public updateClientTask(, PPActivity paramPPActivity, String paramString1, String paramString2, boolean paramBoolean)
    {
      this.activity = paramPPActivity;
      this.checkURL = paramString1;
      this.updateURL = paramString2;
      this.isBackgroundUpdate = paramBoolean;
    }

    protected String doInBackground()
    {
      return UserService.httpGet(this.checkURL);
    }

    protected void onPostExecute()
    {
      AlertDialog.Builder localBuilder;
      int i;
      if (!(this.isBackgroundUpdate))
        this.activity.hideDialog();
      if (paramString == null)
      {
        Log.w("UserService", "update client failed.");
        if (!(this.isBackgroundUpdate))
          Toast.makeText(this.activity.getApplicationContext(), "网络错误，请检查网络是否连接", 0).show();
        return;
      }
      PackageManager localPackageManager = this.activity.getPackageManager();
      try
      {
        do
        {
          PackageInfo localPackageInfo = localPackageManager.getPackageInfo(this.activity.getPackageName(), 0);
          double d1 = Double.valueOf(localPackageInfo.versionName).doubleValue();
          double d2 = Double.valueOf(paramString.trim()).doubleValue();
          localBuilder = new AlertDialog.Builder(this.activity);
          if (d2 == d1)
            break label192;
          i = 1;
          if (i != 0)
            break label198;
          localBuilder.setTitle("客户端已经是最新版本");
          1 local1 = new DialogInterface.OnClickListener(this)
          {
            public void onClick(, int paramInt)
            {
            }
          };
          localBuilder.setPositiveButton(2131361815, local1);
        }
        while (this.isBackgroundUpdate);
        label192: label198: localBuilder.create().show();
      }
      catch (Exception localException)
      {
        while (true)
        {
          while (true)
            Log.e("UserService", localException.toString());
          i = 0;
        }
        String str = this.updateURL + paramString + ".apk";
        localBuilder.setTitle("检测到有新版本，请问是否升级到最新版本? ");
        2 local2 = new DialogInterface.OnClickListener(this, str)
        {
          public void onClick(, int paramInt)
          {
            Intent localIntent = new Intent("android.intent.action.VIEW");
            localIntent.setData(Uri.parse(this.val$downloadURL));
            this.this$1.activity.startActivity(localIntent);
          }
        };
        localBuilder.setPositiveButton(2131361815, local2).setNegativeButton("暂不升级", null);
        localBuilder.create().show();
      }
    }

    protected void onPreExecute()
    {
      if (!(this.isBackgroundUpdate))
        this.activity.showProgressDialog("", "正在检查是否有新版本...");
    }
  }
}